const app = getApp()
Page({
    data: {
      options: [
        {
          text: "删除",
		  bgColor: "#1F2129",
        },
      ],
	  swipeIndex: null,
      list: [],
    },
    onShow(query) {
        this.onList()
// 		this.setData({
// 		    list: [{
//   "id": 175428,
//   "createTime": "2022-09-21 20:58:42",
//   "updateTime": "2022-09-21 20:58:42",
//   "deleteTime": null,
//   "uid": "cfc882bfd505214dd1dcf9a56e73bfb6816b9fe2y4k60hvcl44e",
//   "province": 140000,
//   "provinceStr": "山西省",
//   "city": 140400,
//   "cityStr": "长治市",
//   "area": 140411,
//   "areaStr": "郊区",
//   "street": "山西省-长治市-郊区-黄碾镇魏村",
//   "zcode": null,
//   "telephone": "13613563724",
//   "realname": "李心心",
//   "isDefault": false
// }, {
//   "id": 175429,
//   "createTime": "2022-09-21 20:58:42",
//   "updateTime": "2022-09-21 20:58:42",
//   "deleteTime": null,
//   "uid": "cfc882bfd505214dd1dcf9a56e73bfb6816b9fe2y4k60hvcl44e",
//   "province": 140000,
//   "provinceStr": "山西省",
//   "city": 140400,
//   "cityStr": "长治市",
//   "area": 140411,
//   "areaStr": "郊区",
//   "street": "山西省-长治市-郊区-黄碾镇魏村",
//   "zcode": null,
//   "telephone": "13613563724",
//   "realname": "李心999心",
//   "isDefault": false
// }]
		// })
    },
	 onSwipeStart(e) {
		 console.log(e.index)
	    this.setData({
	      swipeIndex: e.extra,
	    });
	  },
	navigateTo(e){
	    app.navigateTo(e)
	},
    onList() {
      // this.$request("hzsx/userAddress/getUserAddress", {}, "get", true).then(
      //   (res) => {
      //     this.list = res.data
      //   }
      // )
	 let addressList = my.getStorageSync({key: 'addressList'}).data;
	 this.setData({
		 list: addressList ? addressList : [] 
	 })
    },
    onClick(e) {
		const id = this.data.list[e.extra].id
		let addressList = my.getStorageSync({key: 'addressList'}).data;
		addressList.map((item, index)=> {
			if (id == item.id) {
				console.log(index)
				addressList.splice(index, 1)
			}
		})
		my.setStorageSync({
		  key: 'addressList',
		  data: addressList,
		});
		this.onList()
		// setTimeout(()=> {
		// 	this.onList()
		// }, 500)
		// this.$request(
		// 	"hzsx/userAddress/deleteUserAddress",
		// 	{ id },
		// 	"get",
		// 	true
		//   ).then((res) => {
		// 	this.onList()
		//   })
    },
    //获取搜获地址
    getAddress(e) {
		console.log(e.target.dataset.item)
      // this.$setGlobalData("address", data)
      my.navigateBack({
        delta: 1,
      })
    },
    //获取支付宝收货地址
    alipayAddress() {
      if (my.canIUse("getAddress")) {
        my.getAddress({
          success: (res) => {
            const obj = res.result
            if (obj) {
				// console.log(obj)
				let list = my.getStorageSync({key: 'addressList'}).data != null ? my.getStorageSync({key: 'addressList'}).data : []
				let ran = (Math.random() + new Date().getTime()).toString(32).slice(0,8)
				// console.log(ran)
				let item = {
					areaStr: obj.area,
					cityStr: obj.city,
					isDefault: 0,
					provinceStr: obj.prov,
					realname: obj.fullname,
					street: obj.address,
					telephone: obj.mobilePhone,
					id: ran
					// uid: this.$getUid(),
				}
				// console.log(list)
				list.push(item)
				my.setStorageSync({
				  key: 'addressList',
				  data: list,
				});
				this.onList()
              // this.$request(
              //   "hzsx/userAddress/addUserAddress",
              //   {
              //     areaStr: obj.area,
              //     cityStr: obj.city,
              //     isDefault: 0,
              //     provinceStr: obj.prov,
              //     realname: obj.fullname,
              //     street: obj.address,
              //     telephone: obj.mobilePhone,
              //     uid: this.$getUid(),
              //   },
              //   "post"
              // ).then((res) => {
              //   this.onList()
              // })
            }
          },
        })
      }
    },
});